﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace WatchDog
{
    class ListenerThread
    {
        public TcpClient Client { get; set; }
        public NetworkStream NStream { get; set; }
        public byte[] buffer { get; set; }
        public ListenerThread(TcpClient tempclient)
        {
            this.Client = tempclient;
            NStream = this.Client.GetStream();
        }

        public void ListenClientConnect()
        {
            string ReceiveData = "";
            if (NStream.CanRead)
            {
                buffer = new byte[1024];
                int count = 0;
                do
                {
                    //string ReceiveString = null;
                    //MeterIP=99,#99,10001,00111,11100,12341234,44332211,12345678,88889999*
                    //HotelID=210883,MeterIP=99,#210883,00,10001,00011,00110,116300,332211,123456,888999,8152483225296867501373643AB3863F*
                    try
                    {
                        count = NStream.Read(buffer, 0, 1024);
                        //NStream.Flush();
                       
                        ReceiveData = Encoding.Default.GetString(buffer, 0, count);
                        Console.WriteLine(ReceiveData + ":收到数据");
                        //Log.Info("HeartBeat", ReceiveData + ",收到心跳数据");
                        if(ReceiveData== "MyHeartBeat")
                        {
                            Program.LastConnTime = DateTime.Now;
                        }
                        
                        
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message.ToString());
                        Log.Info("ListenClientConnect", ex.Message.ToString());
   
                        break;
                    }
                    //下面的判断为，没有可读数据，并且计数返回的是0 表示客户端有意断开了。
                } while (NStream.DataAvailable || count != 0);
            } 
            Client.Close();
            NStream.Close();
            NStream.Dispose();
            Console.WriteLine("ListenClientConnect线程正常退出");
            //Log.Info("HeartBeat", "对方TCP主动关闭，线程退出");
        }


        public static void CheckThread()
        {
            while (true)
            {
                //先睡眠10分钟
                Thread.Sleep(1000);
                if((DateTime.Now - Program.LastConnTime).TotalSeconds > 20)
                {
                    //关闭进程，再重新开启
                    StopProcess("TcpServer");
                    Log.Info("CheckThread", "关闭了进程");
                    Thread.Sleep(1000);

                    //Process myProcess = Process.Start()
                    //p.Start(@"E:\Project\无隅酒店\TCPServer\TcpServer\bin\Debug\TcpServer.exe");


                    //WinExec(@"E:\Project\无隅酒店\TCPServer\TcpServer\bin\Debug\TcpServer.exe", 3);

                    System.Diagnostics.Process.Start("explorer.exe", "C:\\Users\\Administrator\\Desktop\\NewTcpServer\\TcpServer.exe");
                    //System.Diagnostics.Process.Start("explorer.exe", @"E:\Project\无隅酒店\TCPServer\TcpServer\bin\Debug\TcpServer.exe");
                    Program.LastConnTime = DateTime.Now;
                    Log.Info("CheckThread", "重新开启TcpServer");
                }
                


            }
        }

        public static void StopProcess(string processName)
        {
            try
            {
                System.Diagnostics.Process[] ps = System.Diagnostics.Process.GetProcessesByName(processName);
                foreach (System.Diagnostics.Process p in ps)
                {
                    p.Kill();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        [DllImport("kernel32.dll")]
        public static extern int WinExec(string exeName, int operType);



    }
}
